/*
Query and display NVIDIA VDPAU capabilities, a la glxinfo

Copyright (c) 2008 Wladimir J. van der Laan
Copyright (c) 2014 NVIDIA Corporation

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <dlfcn.h>
#include <vdpau/vdpau.h>
#include <vdpau/vdpau_x11.h>

//#include "query_hw_codecInfo.h"

/******************* Base ****************/

typedef struct vdp_functions {
#define VDP_FUNCTION(vdp_type, _, mp_name) vdp_type *mp_name;
#include "vdpau_functions.inc"
#undef VDP_FUNCTION
VdpDevice device;

}VDP_FUNCTIONS;

/*
void queryBaseInfo(VDP_FUNCTIONS *device)
{
    uint32_t api;
    const char *info;
    if(device->GetApiVersion(&api) != VDP_STATUS_OK ||
       device->GetInformationString(&info) != VDP_STATUS_OK)
    {
        fprintf(stderr, "Error querying API version or information\n");
        exit(-1);
    }
    printf("API version: %i\n", api);
    printf("Information string: %s\n", info);
}
*/

/* Generic description structure */
typedef struct desc_t 
{
    const char *name;
    uint32_t id;
    uint32_t aux; /* optional extra parameter... */
} Desc;

/**************** Video surface ************/
Desc chroma_types[] = {
{"420", VDP_CHROMA_TYPE_420},
{"422", VDP_CHROMA_TYPE_422},
{"444", VDP_CHROMA_TYPE_444},
};
const size_t chroma_type_count = sizeof(chroma_types)/sizeof(Desc);

Desc ycbcr_types[] = {
{"NV12", VDP_YCBCR_FORMAT_NV12},
{"YV12", VDP_YCBCR_FORMAT_YV12},
{"UYVY", VDP_YCBCR_FORMAT_UYVY},
{"YUYV", VDP_YCBCR_FORMAT_YUYV},
{"Y8U8V8A8", VDP_YCBCR_FORMAT_Y8U8V8A8},
{"V8U8Y8A8", VDP_YCBCR_FORMAT_V8U8Y8A8},
};
const size_t ycbcr_type_count = sizeof(ycbcr_types)/sizeof(Desc);

Desc rgb_types[] = {
{"B8G8R8A8", VDP_RGBA_FORMAT_B8G8R8A8},
{"R8G8B8A8", VDP_RGBA_FORMAT_R8G8B8A8},
{"R10G10B10A2", VDP_RGBA_FORMAT_R10G10B10A2},
{"B10G10R10A2", VDP_RGBA_FORMAT_B10G10R10A2},
{"A8", VDP_RGBA_FORMAT_A8},
};
const size_t rgb_type_count = sizeof(rgb_types)/sizeof(Desc);

Desc indexed_types[] = {
{"A4I4", VDP_INDEXED_FORMAT_A4I4},
{"I4A4", VDP_INDEXED_FORMAT_I4A4},
{"A8I8", VDP_INDEXED_FORMAT_A8I8},
{"I8A8", VDP_INDEXED_FORMAT_I8A8},
};
const size_t indexed_type_count = sizeof(indexed_types)/sizeof(Desc);

/*
void queryVideoSurface(VDP_FUNCTIONS *device)
{
    VdpStatus rv;
    printf("\nVideo surface:\n\n");
    printf("name   width height types\n");
    printf("-------------------------------------------\n");
    for(int x=0; x<chroma_type_count; ++x)
    {
        VdpBool is_supported;
        uint32_t max_width, max_height;

        rv = device->video_surface_query_capabilities(device->device, chroma_types[x].id,
            &is_supported, &max_width, &max_height);
        if(rv == VDP_STATUS_OK && is_supported)
        {
            printf("%-6s %5i %5i  ", chroma_types[x].name,
                max_width, max_height);
            
            for(int y=0; y<ycbcr_type_count; ++y)
            {
                rv = device->VideoSurfaceQueryGetPutBitsYCbCrCapabilities(
                    device->device, chroma_types[x].id, ycbcr_types[y].id,
                    &is_supported);
                if(rv == VDP_STATUS_OK && is_supported)
                {
                    printf("%s ", ycbcr_types[y].name);
                }
            }
            printf("\n");
        }
    }
}
*/

/***************** Output surface ****************/
/*
void queryOutputSurface(VDP_FUNCTIONS *device)
{
    VdpStatus rv;
    printf("\nOutput surface:\n\n");
    printf("name              width height nat types\n");
    printf("----------------------------------------------------\n");
    for(int x=0; x<rgb_type_count; ++x)
    {
        VdpBool is_supported, native;
        uint32_t max_width, max_height;

        rv = device->OutputSurfaceQueryCapabilities(device->device, rgb_types[x].id,
            &is_supported, &max_width, &max_height);
        device->OutputSurfaceQueryGetPutBitsNativeCapabilities(device->device, rgb_types[x].id,
            &native);
        if(rv == VDP_STATUS_OK && is_supported)
        {
            printf("%-16s %5i %5i    %c  ", rgb_types[x].name,
                max_width, max_height, native?'y':'-');
           
            for(int y=0; y<ycbcr_type_count; ++y)
            {
                rv = device->OutputSurfaceQueryPutBitsYCbCrCapabilities(
                    device->device, rgb_types[x].id, ycbcr_types[y].id,
                    &is_supported);
                if(rv == VDP_STATUS_OK && is_supported)
                {
                    printf("%s ", ycbcr_types[y].name);
                }
            }

            for(int y=0; y<indexed_type_count; ++y)
            {
                // There is currently only one color table format.  This will
                // have to be reconsidered if additional color table formats are
                // added.
                rv = device->OutputSurfaceQueryPutBitsIndexedCapabilities(
                    device->device, rgb_types[x].id, indexed_types[y].id,
                    VDP_COLOR_TABLE_FORMAT_B8G8R8X8, &is_supported);
                if(rv == VDP_STATUS_OK && is_supported)
                {
                    printf("%s ", indexed_types[y].name);
                }
            }

            printf("\n");
        }
    }
}
*/
/***************** Bitmap surface ****************/

/*
void queryBitmapSurface(VDP_FUNCTIONS *device)
{
    VdpStatus rv;
    printf("\nBitmap surface:\n\n");
    printf("name              width height\n");
    printf("------------------------------\n");
    for(int x=0; x<rgb_type_count; ++x)
    {
        VdpBool is_supported;
        uint32_t max_width, max_height;

        rv = device->BitmapSurfaceQueryCapabilities(device->device, rgb_types[x].id,
            &is_supported, &max_width, &max_height);
        if(rv == VDP_STATUS_OK && is_supported)
        {
            printf("%-16s %5i %5i\n", rgb_types[x].name,
                max_width, max_height);
        }
    }
}
*/

/******************* Video mixer ****************/

/* Type for value ranges */
enum DataType
{
    DT_NONE,
    DT_INT,
    DT_UINT,
    DT_FLOAT
};

Desc mixer_features[] = {
{"DEINTERLACE_TEMPORAL",VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL},
{"DEINTERLACE_TEMPORAL_SPATIAL",VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL},
{"INVERSE_TELECINE",VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE},
{"NOISE_REDUCTION",VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION},
{"SHARPNESS",VDP_VIDEO_MIXER_FEATURE_SHARPNESS},
{"LUMA_KEY",VDP_VIDEO_MIXER_FEATURE_LUMA_KEY},
{"HIGH QUALITY SCALING - L1", VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1},
{"HIGH QUALITY SCALING - L2", VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L2},
{"HIGH QUALITY SCALING - L3", VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L3},
{"HIGH QUALITY SCALING - L4", VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L4},
{"HIGH QUALITY SCALING - L5", VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L5},
{"HIGH QUALITY SCALING - L6", VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L6},
{"HIGH QUALITY SCALING - L7", VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L7},
{"HIGH QUALITY SCALING - L8", VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L8},
{"HIGH QUALITY SCALING - L9", VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L9},
};
const size_t mixer_features_count = sizeof(mixer_features)/sizeof(Desc);

Desc mixer_parameters[] = {
{"VIDEO_SURFACE_WIDTH",VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,DT_UINT},
{"VIDEO_SURFACE_HEIGHT",VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,DT_UINT},
{"CHROMA_TYPE",VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE,DT_NONE},
{"LAYERS",VDP_VIDEO_MIXER_PARAMETER_LAYERS,DT_UINT},
};
const size_t mixer_parameters_count = sizeof(mixer_parameters)/sizeof(Desc);

Desc mixer_attributes[] = {
{"BACKGROUND_COLOR",VDP_VIDEO_MIXER_ATTRIBUTE_BACKGROUND_COLOR,DT_NONE},
{"CSC_MATRIX",VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX,DT_NONE},
{"NOISE_REDUCTION_LEVEL",VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL,DT_FLOAT},
{"SHARPNESS_LEVEL",VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL,DT_FLOAT},
{"LUMA_KEY_MIN_LUMA",VDP_VIDEO_MIXER_ATTRIBUTE_LUMA_KEY_MIN_LUMA,DT_NONE},
{"LUMA_KEY_MAX_LUMA",VDP_VIDEO_MIXER_ATTRIBUTE_LUMA_KEY_MAX_LUMA,DT_NONE},
};
const size_t mixer_attributes_count = sizeof(mixer_attributes)/sizeof(Desc);

/*

void display_range(uint32_t aux, uint32_t minval, uint32_t maxval)
{
    switch(aux)
    {
        case DT_INT: printf("%8u %8u", minval, maxval); break;
        case DT_UINT: printf("%8u %8u", minval, maxval); break;
        case DT_FLOAT: printf("%8.2f %8.2f", *((float*)&minval), *((float*)&maxval)); break;
        default;
    }
}


void queryVideoMixer(VDP_FUNCTIONS *device)
{
    VdpStatus rv;
    printf("\nVideo mixer:\n\n");
   // Features
    printf("feature name                    sup\n");
    printf("------------------------------------\n");
    for(int x=0; x<mixer_features_count; ++x)
    {
        VdpBool is_supported;

        rv = device->VideoMixerQueryFeatureSupport(device->device, mixer_features[x].id,
            &is_supported);
        is_supported = (rv == VDP_STATUS_OK && is_supported);
        printf("%-32s %c\n", mixer_features[x].name,
            is_supported?'y':'-');

    }
    printf("\n");
    // Parameters (+range)
    printf("parameter name                  sup      min      max\n");
    printf("-----------------------------------------------------\n");
    for(int x=0; x<mixer_parameters_count; ++x)
    {
        VdpBool is_supported;

        rv = device->VideoMixerQueryParameterSupport(device->device, mixer_parameters[x].id,
            &is_supported);
        is_supported = (rv == VDP_STATUS_OK && is_supported);
        printf("%-32s %c  ", mixer_parameters[x].name,
            is_supported?'y':'-');
 
        if(is_supported && mixer_parameters[x].aux != DT_NONE)
        {
            uint32_t minval, maxval;
            rv = device->VideoMixerQueryParameterValueRange(device->device, mixer_parameters[x].id,
                (void*)&minval, (void*)&maxval);
            if(rv == VDP_STATUS_OK)
                display_range(mixer_parameters[x].aux, minval, maxval);
        }
        printf("\n");
    }
    printf("\n");

    // Attributes (+range)
    printf("attribute name                  sup      min      max\n");
    printf("-----------------------------------------------------\n");
    for(int x=0; x<mixer_attributes_count; ++x)
    {
        VdpBool is_supported;

        rv = device->VideoMixerQueryAttributeSupport(device->device, mixer_attributes[x].id,
            &is_supported);
        is_supported = (rv == VDP_STATUS_OK && is_supported);
        printf("%-32s %c  ", mixer_attributes[x].name,
            is_supported?'y':'-');
       
        if(is_supported && mixer_attributes[x].aux != DT_NONE)
        {
            uint32_t minval, maxval;
            rv = device->VideoMixerQueryAttributeValueRange(device->device, mixer_parameters[x].id,
                (void*)&minval, (void*)&maxval);
            if(rv == VDP_STATUS_OK)
                display_range(mixer_attributes[x].aux, minval, maxval);
        }
        printf("\n");
    }
    printf("\n");
}
*/

/******************* Decoder ****************/

Desc decoder_profiles[] = {
{"MPEG1",              VDP_DECODER_PROFILE_MPEG1},
{"MPEG2_SIMPLE",       VDP_DECODER_PROFILE_MPEG2_SIMPLE},
{"MPEG2_MAIN",         VDP_DECODER_PROFILE_MPEG2_MAIN},
{"H264_BASELINE",      VDP_DECODER_PROFILE_H264_BASELINE},
{"H264_MAIN",          VDP_DECODER_PROFILE_H264_MAIN},
{"H264_HIGH",          VDP_DECODER_PROFILE_H264_HIGH},
{"VC1_SIMPLE",         VDP_DECODER_PROFILE_VC1_SIMPLE},
{"VC1_MAIN",           VDP_DECODER_PROFILE_VC1_MAIN},
{"VC1_ADVANCED",       VDP_DECODER_PROFILE_VC1_ADVANCED},
{"MPEG4_PART2_SP",     VDP_DECODER_PROFILE_MPEG4_PART2_SP},
{"MPEG4_PART2_ASP",    VDP_DECODER_PROFILE_MPEG4_PART2_ASP},
{"DIVX4_QMOBILE",      VDP_DECODER_PROFILE_DIVX4_QMOBILE},
{"DIVX4_MOBILE",       VDP_DECODER_PROFILE_DIVX4_MOBILE},
{"DIVX4_HOME_THEATER", VDP_DECODER_PROFILE_DIVX4_HOME_THEATER},
{"DIVX4_HD_1080P",     VDP_DECODER_PROFILE_DIVX4_HD_1080P},
{"DIVX5_QMOBILE",      VDP_DECODER_PROFILE_DIVX5_QMOBILE},
{"DIVX5_MOBILE",       VDP_DECODER_PROFILE_DIVX5_MOBILE},
{"DIVX5_HOME_THEATER", VDP_DECODER_PROFILE_DIVX5_HOME_THEATER},
{"DIVX5_HD_1080P",     VDP_DECODER_PROFILE_DIVX5_HD_1080P},
{"H264_CONSTRAINED_BASELINE",   VDP_DECODER_PROFILE_H264_CONSTRAINED_BASELINE},
{"H264_EXTENDED",               VDP_DECODER_PROFILE_H264_EXTENDED},
{"H264_PROGRESSIVE_HIGH",       VDP_DECODER_PROFILE_H264_PROGRESSIVE_HIGH},
{"H264_CONSTRAINED_HIGH",       VDP_DECODER_PROFILE_H264_CONSTRAINED_HIGH},
{"H264_HIGH_444_PREDICTIVE",    VDP_DECODER_PROFILE_H264_HIGH_444_PREDICTIVE},
{"HEVC_MAIN",                   VDP_DECODER_PROFILE_HEVC_MAIN},
{"HEVC_MAIN_10",                VDP_DECODER_PROFILE_HEVC_MAIN_10},
{"HEVC_MAIN_STILL",             VDP_DECODER_PROFILE_HEVC_MAIN_STILL},
{"HEVC_MAIN_12",                VDP_DECODER_PROFILE_HEVC_MAIN_12},
{"HEVC_MAIN_444",               VDP_DECODER_PROFILE_HEVC_MAIN_444},
};
const size_t decoder_profile_count = sizeof(decoder_profiles)/sizeof(Desc);

void queryDecoderCaps(VDP_FUNCTIONS *device)
{
    VdpStatus rv;
    printf("\nDecoder capabilities:\n\n");
    printf("name                        level macbs width height\n");
    printf("----------------------------------------------------\n");
    for(int x=0; x<decoder_profile_count; ++x)
    {
        VdpBool is_supported;
        uint32_t max_level, max_macroblocks, max_width, max_height;

        rv = device->decoder_query_capabilities(device->device, decoder_profiles[x].id,
            &is_supported, &max_level, &max_macroblocks, &max_width, &max_height);
        if(rv == VDP_STATUS_OK && is_supported)
        {
            printf("%-30s %2u %5u %5u %5u\n", decoder_profiles[x].name,
                max_level, max_macroblocks, max_width, max_height);
        } else {
            printf("%-30s --- not supported ---\n", decoder_profiles[x].name);
        }
    }
}

/*
static void usage(const char *argv0)
{
    printf("Usage: %s [--display displayname] [--screen screen_number]\n", argv0);
    printf("  --display displayname     Server to query\n");
    printf("  --screen screen_number    X screen to query\n");
    printf("  --help                    Print this help text\n");
}
*/

static void invalid_argument(const char *argv0, const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);

    fprintf(stderr, "%s: ", argv0);
    vfprintf(stderr, fmt, ap);
    fprintf(stderr, "Try '%s --help' for more information.\n", argv0);

    va_end(ap);
}

struct Options {
    char *display_name;
    int screen;
};

static struct Options parse_options(int argc, char **argv)
{
    struct Options o;

    o.display_name = XDisplayName(NULL);
    o.screen = -1;
/*
    for(int i = 1; i < argc;)
    {
        const char *arg = argv[i++];

        if(strcmp(arg, "--display") == 0)
        {
            if(i >= argc)
                invalid_argument(argv[0], "--display requires an argument\n");
            o.display_name = XDisplayName(argv[i++]);
        }
        else if(strcmp(arg, "--screen") == 0)
        {
            if(i >= argc)
                invalid_argument(argv[0], "--screen requires an argument\n");

            char *end;
            o.screen = strtol(argv[i], &end, 0);
            if(end == argv[i] || *end != '\0')
                invalid_argument(argv[0], "could not parse '%s' as a number\n", argv[i]);
            if(o.screen < 0)
                invalid_argument(argv[0], "--screen argument cannot be negative\n");
            i++;
        }
        else if(strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0)
        {
            usage(argv[0]);
        }
        else
        {
            invalid_argument(argv[0], "unrecognized option '%s'\n", arg);
        }
    }
*/
    return o;
}

int query_vdpauinfo()
{
    /* Parse options */
    // struct Options o = parse_options(argc, argv);
    const char* name="vdpauinfo";

    /* Create an X Display */
    Display *display = XOpenDisplay(NULL);
    if(!display)
    {
        fprintf(stderr,"%s: cannot connect to X server\n",name);
        return -1;
    }

    int screen = DefaultScreen(display);
    if(screen >= ScreenCount(display)) {
        invalid_argument(name, "screen %d requested but X server only has %d screen%s\n",
                         screen, ScreenCount(display),
                         ScreenCount(display) == 1 ? "" : "s");
        return -1;
    }
    printf("display: %p   screen: %i\n",display,screen);



    struct vdp_functions vdp = {0};
    
    struct vdp_function {
        const int id;
        int offset;
    };

    static const struct vdp_function vdp_func[] = {
#define VDP_FUNCTION(_, macro_name, mp_name) {macro_name, offsetof(struct vdp_functions, mp_name)},
#include "vdpau_functions.inc"
#undef VDP_FUNCTION
            {0, -1}
    };
        

    /* Create device */
    VdpDevice device;
    VdpGetProcAddress *get_proc_address;
    VdpStatus rv;
    rv = vdp_device_create_x11(display, screen, &device, &get_proc_address);
    if(rv != VDP_STATUS_OK)
    {
        fprintf(stderr, "Error creating VDPAU device: %i\n", rv); /* cannot use GetErrorString here */
        return -1;
    }

    VdpStatus vdp_st;
    
    for (const struct vdp_function *dsc = vdp_func; dsc->offset >= 0; dsc++) {
        vdp_st = get_proc_address(device, dsc->id,
                                  (void **)((char *)&vdp + dsc->offset));
        if (vdp_st != VDP_STATUS_OK) {
            printf("Error when calling vdp_get_proc_address(function "
                   "id %d): %s\n",  dsc->id,
                   vdp.get_error_string ? vdp.get_error_string(vdp_st) : "?");
            return -1;
        }
    }
    vdp.device = device;

    //ctx->vdp = vdp;
    //ctx->get_proc_address = get_proc_address;



    //VDPDeviceImpl *impl = new VDPDeviceImpl(device, get_proc_address);

    //queryBaseInfo(impl);
    //queryVideoSurface(&vdp);
    queryDecoderCaps(&vdp);
    //queryOutputSurface(&vdp);
    //queryBitmapSurface(&vdp);
    //queryVideoMixer(&vdp);
    VdpDeviceDestroy *pvdp_device_destroy;

    vdp_st = get_proc_address(device, VDP_FUNC_ID_DEVICE_DESTROY, (void**)&pvdp_device_destroy);
    if (vdp_st != VDP_STATUS_OK) {
        fprintf(stderr, "Failed to find the VdpDeviceDestroy function: %d\n", vdp_st);
        return -1;
    }
    pvdp_device_destroy(device);
    XCloseDisplay(display);
    printf("\n");
    return 0;
}

int test_vdpau() 
{
    #if 0
  int ret = 0;
  Display *display = XOpenDisplay(NULL);
  VdpDevice device;
  VdpGetProcAddress *get_proc_address = NULL;
  if (!display) {
    fprintf(stderr, "test_vdpau: cannot connect to X server\n");
    ret = -1;
  }
  int screen = DefaultScreen(display);
  ret = vdp_device_create_x11(display, screen, &device, &get_proc_address);
  if (ret != VDP_STATUS_OK || get_proc_address == NULL) {
    fprintf(stderr, "Error creating VDPAU device: %d\n", ret);
    ret = -1;
  }
  XCloseDisplay(display);
  return ret;
#endif
    int ret = 0;
    const char* libXext_so="libXext.so.6";
    const char* libvdpau_so="libvdpau.so";
    // void *libXext = dlopen(libXext_so, RTLD_LAZY);
    void *libvdpau = dlopen(libvdpau_so, RTLD_LAZY);
    Display *dpy = XOpenDisplay(NULL);
    VdpDeviceCreateX11 *pvdp_device_create_x11;
    VdpDevice device;
    VdpGetProcAddress *get_proc_address;
    VdpStatus status;

    // if (!libXext) {
    //     fprintf(stderr, "Failed to open %s: %s", libXext_so, dlerror());
    //     return -2;
    // }
    if (!dpy) {
        fprintf(stderr, "Failed to connect to X display %s\n", XDisplayName(NULL));
        return -1;
    }

    if (!libvdpau) {
        fprintf(stderr, "Failed to open %s: %s",libvdpau_so, dlerror());
        ret = -1;
        goto fail;
    }
    pvdp_device_create_x11 = dlsym(libvdpau, "vdp_device_create_x11");
    if (!pvdp_device_create_x11) {
        fprintf(stderr, "Failed to find the symbol vdp_device_create_x11\n");
        ret = -1;
        goto fail;
    }
#if 1
    //now unkown reson will occur issue that it make openframe test process
    //exit,so comment it
    int screen = DefaultScreen(dpy);
    status = pvdp_device_create_x11(dpy, screen, &device, &get_proc_address);
    if (status == VDP_STATUS_OK) {
        // It's okay if creating the device fails.  This will still install the
        // DRI2 extension in libX11 and trigger the bug.
        VdpDeviceDestroy *pvdp_device_destroy;

        status = get_proc_address(device, VDP_FUNC_ID_DEVICE_DESTROY, (void**)&pvdp_device_destroy);
        if (status != VDP_STATUS_OK) {
            fprintf(stderr, "Failed to find the VdpDeviceDestroy function: %d\n", status);
            ret = -1;
            goto fail;
        }
        status = pvdp_device_destroy(device);
        if (status != VDP_STATUS_OK) {
            fprintf(stderr, "Failed to excute the VdpDeviceDestroy function: %d\n", status);
            ret = -1;
            goto fail;
        }
    }
#endif
fail:
  if (libvdpau) dlclose(libvdpau);
  if (dpy) XCloseDisplay(dpy);
  return ret;
}
